<?php

namespace addons\ymyauexpress\library;

use fast\Http;
use think\Exception;

class Expressty
{
    public $APIID = 'E55SQn87';
    public $APIKEY = '866607858B9CBF7B205AB8489FA06DD3';
    protected $error = null;
    public $params = array();
    private $create_api = "https://api.ytoglobal.com.au/CreateOrder.ashx";
    private $upload_api = "https://api.ytoglobal.com.au/UploadIDCard.ashx";
    private $track_api = "https://api.ytoglobal.com.au/OrderTracklog.ashx";

    public function __construct()
    {
        $timestamp = time();
        $this->params = [
            'apikey' => $this->APIID,
            'secretkey' => $this->APIKEY,
            'version' => "v3.0",
            'timestamp' => $timestamp,
            'sign' => strtoupper(md5('apikey'.$this->APIID . 'secretkey'.$this->APIKEY . "versionv3.0"  .'timestamp'.$timestamp)),
        ];
    }


    //创建订单
    public function create($p)
    {
         $para = $p;
         $para['apikey'] = $this->params['apikey'];
         $para['secretkey'] = $this->params['secretkey'];
         $para['version'] = $this->params['version'];
         $para['timestamp'] = $this->params['timestamp'];
         $para['sign'] = $this->params['sign'];
            $url = $this->create_api;
            $result = $this->postRequest($url,$para);
            return $result;
    }

    // 上传身份证
    public function upload($params)
    {
        $para = $params;
        $para['apikey'] = $this->params['apikey'];
        $para['secretkey'] = $this->params['secretkey'];
        $para['version'] = $this->params['version'];
        $para['timestamp'] = $this->params['timestamp'];
        $para['sign'] = $this->params['sign'];
        $result = $this->postRequest($this->upload_api, $para);
        if ($result['code'] == 0) {
            return true;
        } else {
            return false;
        }
    }

    // 物流

    /**
     * @param $logisticsno
     * @return mixed
     * {
    "code":"1",
    "TrackLogList":[
    {
    "eventCode":"GOT",
    "Time":"2021/11/2 8:24:13",
    "Content":"澳大利亚1032网点,已揽收"
    }
    ],
    "message":"获取轨迹成功"
    }
     *
     *
     *  'time' => strtotime($vo['StatusTime']),
      *   'status' => '在途'/*$vo['id_OrderStatus'], // 1.0.6升级
       * 'context' => $vo['StatusDetail'],
     *
     *
     */
    public function track($logisticsno)
    {
        $arr  = [];
        $this->params['logisticsno'] = $logisticsno;
        $url = $this->track_api;
        $result = $this->postRequest($url,$this->params);
         foreach ($result['TrackLogList'] as $value){
              $arr[] = [
                  'StatusTime'=>$value['Time'],
                  'StatusDetail'=>$value['Content']
              ];
         }
         return $arr;
    }



    // post请求
    protected function postRequest($url, $params = [])
    {
        $params = json_encode($params);
        $header = [
            'Content-Type: application/json',
        ];
        $result = Http::post($url, $params);
        return json_decode($result,true);
    }

    // get请求
    protected function getRequest($url, $params = [])
    {
        $params = json_encode($params, JSON_UNESCAPED_UNICODE);
        $header = [
            'Content-Type: application/json',
            'Authorization: Bearer ' . $this->token];
        $result = Http::get($url, $params, [
            CURLOPT_HTTPHEADER => $header,
        ]);
        return json_decode($result, true);
    }

    // file请求
    protected function fileRequest($url, $filename)
    {

        $fp = fopen($filename, 'w+');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $this->token]);
        dump(curl_exec($ch));
        curl_close($ch);
    }

    function posturl($url, $data)
    {
        $data = json_encode($data);
        $headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);
    }

}